package com.smartwebx.ha.biz.rainwater.real.impl;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.smartwebx.ha.biz.rainwater.feature.FeatureRiverManager;
import com.smartwebx.ha.biz.rainwater.real.ProcessDataManager;
import com.smartwebx.ha.biz.rainwater.real.RiverFloodControlManager;
import com.smartwebx.ha.biz.rainwater.real.RiverManager;
import com.smartwebx.ha.dal.feature.dao.FeatureRiverMapper;
import com.smartwebx.ha.dal.realtime.dao.RiverMapper;
import com.smartwebx.ha.dal.realtime.dao.handler.ProcessDataEnum;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.FeatureHighLow;
import com.smartwebx.ha.dal.usebean.RiverFC;
import com.smartwebx.ha.dal.usebean.RiverFlood;
import com.smartwebx.ha.dal.usebean.WaterYearComp;
import com.smartwebx.ha.dal.usebean.enu.FlowTypeEnum;
import com.smartwebx.ha.dal.usebean.view.ViewDayProcessAnalysis;
import com.smartwebx.ha.dal.util.DateFullProcess;
import com.smartwebx.ha.dal.util.core.WaterProcessDatasAnalysis;

@Service
public class RiverManagerImpl implements RiverManager {
	@Autowired
	private RiverMapper riverDao;
	@Autowired
	private FeatureRiverMapper featureRiverDao;
	@Autowired
	private ProcessDataManager processDataService;
	@Autowired
	private FeatureRiverManager featureRiverService;
	@Autowired
	private RiverFloodControlManager riverFloodControlService;

	@Override
	public List<DateValue> getSingleStatWaterHourProcess(String stcd,
			Date startHour, Date endHour) {
		return WaterProcessDatasAnalysis.getSingleStatHourProcess(riverDao,
				stcd, startHour, endHour, "water");
	}

	@Override
	public List<DateValue> getSingleStatWaterDayProcess(String stcd,
			Date startDate, Date endDate) {
		return WaterProcessDatasAnalysis.getSingleStatDayProcess(riverDao,
				stcd, startDate, endDate, "water");
	}

	@Override
	public List<DateValue> getPurenessWaterProcesss(String stcd,
			Date startDate, Date endDate) {
		return WaterProcessDatasAnalysis.getPurenessProcesss(riverDao, stcd,
				startDate, endDate, "water");
	}

	@Override
	public WaterYearComp getMoreYearWaterCompSort(String stcd, Date startDate,
			Date endDate) {
		Iterable<DateValue> maxMinAvgIter = processDataService
				.getWaterProcessMaxMinAvg(startDate, endDate,
						ProcessDataEnum.HE_DAO, stcd);
		return WaterProcessDatasAnalysis.getMoreYearCompSort(featureRiverDao,
				stcd, maxMinAvgIter, "water");
	}

	@Override
	public List<DateValue> getSingleStatFlowHourProcess(String stcd,
			Date startHour, Date endHour) {
		return WaterProcessDatasAnalysis.getSingleStatHourProcess(riverDao,
				stcd, startHour, endHour, "flow");
	}

	@Override
	public List<DateValue> getSingleStatFlowDayProcess(String stcd,
			Date startDate, Date endDate) {
		return WaterProcessDatasAnalysis.getSingleStatDayProcess(riverDao,
				stcd, startDate, endDate, "flow");
	}

	@Override
	public List<DateValue> getPurenessFlowProcesss(String stcd, Date startDate,
			Date endDate) {
		return WaterProcessDatasAnalysis.getPurenessProcesss(riverDao, stcd,
				startDate, endDate, "flow");
	}

	@Override
	public WaterYearComp getMoreYearFlowCompSort(String stcd, Date startDate,
			Date endDate) {
		Iterable<DateValue> maxMinAvgIter = processDataService
				.getFlowProcessMaxMinAvg(startDate, endDate,
						ProcessDataEnum.HE_DAO, stcd);
		return WaterProcessDatasAnalysis.getMoreYearCompSort(featureRiverDao,
				stcd, maxMinAvgIter, "flow");
	}

	@Override
	public String getMoreDayFlowerCount(String stcd, Date startDate,
			Date endDate, Character minus) {
		startDate = DateUtils.addDays(startDate, 1);
		endDate = DateUtils.addDays(endDate, 1);

		List<DateValue> dayFlowers = riverDao.getDayFlowerProcess(stcd,
				startDate, endDate, FlowTypeEnum.AVGFLOWER);

		List<DateValue> hourFlowers = riverDao.getDayFlowerProcess(stcd,
				startDate, endDate, FlowTypeEnum.DAYFLOWER);
		Set<Date> dateSets = DateFullProcess.returnDateSectStr(startDate,
				endDate).keySet();
		Double flowerSum = 0d;
		Boolean haveDate = true;
		Set<Date> onDayNotDate = Sets.newHashSet();

		for (Date date : dateSets) {
			for (DateValue dayDv : dayFlowers) {
				haveDate = true;
				Date oneDay = dayDv.getTm();
				Double val = dayDv.getVal();
				if (Objects.equal(oneDay, date)) {
					if (minus == '+' && val > 0) {
						flowerSum += val;
					} else if (minus == '-' && val < 0) {
						flowerSum += val;
					}

					haveDate = false;
					break;
				}
			}

			if (haveDate) {
				onDayNotDate.add(date);
			}
		}

		for (Date notDate : onDayNotDate) {
			for (DateValue hourDv : hourFlowers) {
				Date hourTm = hourDv.getTm();
				Double hourVal = hourDv.getVal();

				Date notDate8 = DateUtils.setHours(notDate, 8);
				Date notDate16 = DateUtils.setHours(notDate, 16);
				if (Objects.equal(hourTm, notDate8)
						|| Objects.equal(hourTm, notDate16)) {
					if (minus == '+' && hourVal > 0) {
						flowerSum += hourVal;
					} else if (minus == '-' && hourVal < 0) {
						flowerSum += hourVal;
					}
				}
			}
		}

		return String.valueOf(flowerSum);
	}

	@Override
	public RiverFlood getRiverFlood(String stcd) {
		return riverDao.getRiverFlood(stcd);
	}

	@Override
	public ViewDayProcessAnalysis getDayProcessAnalysis(String stcd,
			Date startDate, Date endDate) {
		// 最高最低水位
		FeatureHighLow hisHighLowWater = featureRiverService
				.getMoreYearStatHighLowWater(stcd);
		// 最高最低流量
		FeatureHighLow hisHighLowFlow = featureRiverService
				.getMoreYearStatHighLowFlow(stcd);
		// 水位最高最低平均
		Iterable<DateValue> waterMaxMinAvg = processDataService
				.getWaterProcessMaxMinAvg(startDate, endDate,
						ProcessDataEnum.HE_DAO, stcd);
		// 流量最高最低
		Iterable<DateValue> flowMaxMinAvg = processDataService
				.getFlowProcessMaxMinAvg(startDate, endDate,
						ProcessDataEnum.HE_DAO, stcd);
		// 防洪信息
		RiverFC riverFC = riverFloodControlService
				.getSingleRiverFloodControl(stcd);
		int exceedWarnDay = 0;
		if (riverFC != null) {
			exceedWarnDay = processDataService.getWaterExceedWarnDays(
					startDate, endDate, ProcessDataEnum.HE_DAO, stcd,
					riverFC.getWarWater());
		}
		// 水位多年历史比较
		WaterYearComp waterYearComp = this.getMoreYearWaterCompSort(stcd,
				startDate, endDate);
		// 流量多年历史比较
		WaterYearComp flowYearComp = this.getMoreYearFlowCompSort(stcd,
				startDate, endDate);


		ViewDayProcessAnalysis viewDayProcessAnal = new ViewDayProcessAnalysis();
		// /////////水位////////////
		// 历史最高水位
		viewDayProcessAnal.setHisMaxWater(new DateValue(hisHighLowWater
				.getMaxzTm(), hisHighLowWater.getMaxz()));
		// 历史最低水位
		viewDayProcessAnal.setHisMinWater(new DateValue(hisHighLowWater
				.getMinzTm(), hisHighLowWater.getMinz()));
		// 最高水位
		viewDayProcessAnal.setMaxWater(Iterables.get(waterMaxMinAvg, 0));
		// 最低水位
		viewDayProcessAnal.setMinWater(Iterables.get(waterMaxMinAvg, 1));
		// 当前平均水位
		viewDayProcessAnal.setCurAvgWater(Iterables.get(waterMaxMinAvg, 2));
		// 历史平均水位
		viewDayProcessAnal.setHisAvgWater(waterYearComp.getAvgHisValue());
		// 平均水位排位
		viewDayProcessAnal.setAvgWaterSort(waterYearComp.getAvgSotrHistory());
		// 水位最大值序列
		viewDayProcessAnal.setWaterMaxs(waterYearComp.getMaxHisArray());
		// 水位最小值序列
		viewDayProcessAnal.setWaterMins(waterYearComp.getMinHisArray());
		// 水位平均值序列
		viewDayProcessAnal.setWaterAvgs(waterYearComp.getAvgHisArray());
		// 最高水位历史排位
		viewDayProcessAnal.setMaxWaterSort(waterYearComp.getMaxSotrHistory());
		// 最低水位历史排位
		viewDayProcessAnal.setMinWaterSort(waterYearComp.getMinSotrHistory());
		
		// ////////////流量//////////////////

		// 历史最大流量
		viewDayProcessAnal.setHisMaxFlow(new DateValue(hisHighLowFlow
				.getMaxzTm(), hisHighLowFlow.getMaxz()));
		// 历史最小流量
		viewDayProcessAnal.setHisMinFlow(new DateValue(hisHighLowFlow
				.getMinzTm(), hisHighLowFlow.getMinz()));
		// 最大流量
		viewDayProcessAnal.setMaxFlow(Iterables.get(flowMaxMinAvg, 0));
		// 最小流量
		viewDayProcessAnal.setMinFlow(Iterables.get(flowMaxMinAvg, 1));
		// 当前平均流量
		viewDayProcessAnal.setCurAvgFlow(Iterables.get(flowMaxMinAvg, 2));
		// 历史平均流量
		viewDayProcessAnal.setHisAvgFlow(flowYearComp.getAvgHisValue());
		// 平均流量排位
		viewDayProcessAnal.setAvgFlowSort(flowYearComp.getAvgSotrHistory());
		// 流量最大值序列
		viewDayProcessAnal.setFlowMaxs(flowYearComp.getMaxHisArray());
		// 流量最小值序列
		viewDayProcessAnal.setFlowMins(flowYearComp.getMinHisArray());
		// 流量平均值序列
		viewDayProcessAnal.setFlowAvgs(flowYearComp.getAvgHisArray());
		// 最大流量历史排位
		viewDayProcessAnal.setMaxFlowSort(flowYearComp.getMaxSotrHistory());
		// 最小流量历史排位
		viewDayProcessAnal.setMinFlowSort(flowYearComp.getMinSotrHistory());
		// 警戒天数
		viewDayProcessAnal.setWarDayNum(exceedWarnDay);
		
		return viewDayProcessAnal;
	}

}
